import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { NotFoundComponent } from '../not-found/not-found.component';
import { HomeComponent } from '../home/home.component';
import { loadRemoteModule } from '@angular-architects/module-federation';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root',
})
export class PluginService {

  constructor(
      private router: Router,
  ) {
  }

  init() {
    //演示使用，项目中可以调整为从后台读取信息，然后组装成路由配置对象
    return new Promise<void>(resolve => {
      this.router.resetConfig([
        {path: '', component: HomeComponent, pathMatch: 'full'},
        {
          path: 'plugin1', loadChildren: () => loadRemoteModule({
            type: 'module',
            remoteEntry: environment.production ? '/assets/plugin1/remoteEntry.js' : 'http://localhost:4201/remoteEntry.js',
            exposedModule: './OrderModule',
          }).then(m => m.OrderModule),
        },
        {path: '**', component: NotFoundComponent},
      ]);
      resolve();
    });
  }
}
